package com.lemon.ui.codemaker.JacksonParser;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang3.StringUtils;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.lemon.components.ant.project.beans.ModelInfo;
import com.lemon.components.ant.project.makeservice.beanparser.JsonParser;
import com.lemon.components.ant.project.makeservice.beanparser.RemarkGetter;
import com.lemon.components.ant.project.makeservice.beanparser.RemarkGetter.ColumnIndex;
import com.lemon.components.common.result.ResultBase;

public class JacksonParserUI extends JDialog {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			JacksonParserUI dialog = new JacksonParserUI();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final JPanel contentPanel = new JPanel();
	private JTextField packageText;
	private JTextField txtTest;
	private JComboBox<String> comboBox;
	private JTextArea textArea1;
	private JTextArea textArea2;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_3;
	private JTextField textField_2;
	private JTextField textField_4;
	private JTextField textField_5;

	/**
	 * Create the dialog.
	 */
	public JacksonParserUI() {
		setType(Type.POPUP);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setResizable(false);
		setTitle("JSON转类");
		setModal(true);
		setBounds(100, 100, 555, 570);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(87dlu;default):grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(81dlu;default):grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		{
			JLabel lblNewLabel = new JLabel("类名称");
			contentPanel.add(lblNewLabel, "2, 2");
		}
		{
			txtTest = new JTextField();
			contentPanel.add(txtTest, "4, 2, 3, 1, fill, default");
			txtTest.setColumns(10);
		}
		{
			JLabel lblNewLabel_2 = new JLabel("Maven模块");
			contentPanel.add(lblNewLabel_2, "10, 2, right, default");
		}
		{
			comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(new String[] {"biz", "facade", "integration", "web"}));
			contentPanel.add(comboBox, "14, 2, fill, default");
		}
		{
			JLabel lblNewLabel_1 = new JLabel("包路径");
			contentPanel.add(lblNewLabel_1, "2, 4");
		}
		{
			packageText = new JTextField();
			contentPanel.add(packageText, "4, 4, 9, 1, fill, default");
			packageText.setColumns(10);
		}
		{
			JButton btnNewButton = new JButton("选择包");
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					PackageSelect packageSelect = new PackageSelect((String)comboBox.getSelectedItem());
					packageSelect.setLocationRelativeTo(contentPanel);
					packageSelect.setVisible(true);
					if(StringUtils.isNotBlank(packageSelect.getSelectedValue())) {
						packageText.setText(StringUtils.uncapitalize(packageSelect.getSelectedValue()));
					}
				}
			});
			contentPanel.add(btnNewButton, "14, 4");
		}
		{
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			contentPanel.add(tabbedPane, "2, 6, 15, 1, fill, fill");
			{
				JPanel panel = new JPanel();
				panel.setToolTipText("JSON");
				tabbedPane.addTab("JSON字符串", null, panel, null);
				panel.setLayout(new BorderLayout(0, 0));
				{
					JScrollPane scrollPane = new JScrollPane();
					panel.add(scrollPane, BorderLayout.CENTER);
					{
						textArea1 = new JTextArea();
						scrollPane.setViewportView(textArea1);
					}
				}
			}
			{
				JPanel panel = new JPanel();
				tabbedPane.addTab("注释字符串", null, panel, null);
				panel.setLayout(new BorderLayout(0, 0));
				{
					JScrollPane scrollPane = new JScrollPane();
					panel.add(scrollPane, BorderLayout.CENTER);
					{
						textArea2 = new JTextArea();
						scrollPane.setViewportView(textArea2);
					}
					{
						JPanel panel_1 = new JPanel();
						scrollPane.setColumnHeaderView(panel_1);
						panel_1.setLayout(new FormLayout(new ColumnSpec[] {
								FormSpecs.RELATED_GAP_COLSPEC,
								FormSpecs.DEFAULT_COLSPEC,
								FormSpecs.RELATED_GAP_COLSPEC,
								ColumnSpec.decode("default:grow"),
								FormSpecs.RELATED_GAP_COLSPEC,
								FormSpecs.DEFAULT_COLSPEC,
								FormSpecs.DEFAULT_COLSPEC,
								ColumnSpec.decode("54px:grow"),},
							new RowSpec[] {
								FormSpecs.RELATED_GAP_ROWSPEC,
								FormSpecs.DEFAULT_ROWSPEC,
								FormSpecs.RELATED_GAP_ROWSPEC,
								FormSpecs.DEFAULT_ROWSPEC,
								FormSpecs.RELATED_GAP_ROWSPEC,
								FormSpecs.DEFAULT_ROWSPEC,
								FormSpecs.RELATED_GAP_ROWSPEC,
								FormSpecs.DEFAULT_ROWSPEC,
								FormSpecs.LINE_GAP_ROWSPEC,
								RowSpec.decode("15px:grow"),}));
						{
							JLabel lblNewLabel_6 = new JLabel("地址：");
							panel_1.add(lblNewLabel_6, "2, 2, right, default");
						}
						{
							textField_2 = new JTextField();
							panel_1.add(textField_2, "4, 2, fill, default");
							textField_2.setColumns(10);
						}
						{
							JLabel lblNewLabel_7 = new JLabel("CssQuery：");
							panel_1.add(lblNewLabel_7, "7, 2, right, default");
						}
						{
							textField_3 = new JTextField();
							panel_1.add(textField_3, "8, 2, fill, default");
							textField_3.setColumns(10);
						}
						{
							JLabel lblNewLabel_4 = new JLabel("CssColumn：");
							panel_1.add(lblNewLabel_4, "2, 4, right, default");
						}
						{
							textField_1 = new JTextField();
							panel_1.add(textField_1, "4, 4, fill, default");
							textField_1.setColumns(10);
						}
						{
							JLabel lblNewLabel_3 = new JLabel("code索引：");
							panel_1.add(lblNewLabel_3, "7, 4, right, top");
						}
						{
							textField = new JTextField();
							panel_1.add(textField, "8, 4, fill, default");
							textField.setColumns(10);
						}
						{
							JLabel lblNewLabel_5 = new JLabel("Remark索引：");
							panel_1.add(lblNewLabel_5, "2, 6, right, default");
						}
						{
							textField_4 = new JTextField();
							panel_1.add(textField_4, "4, 6, fill, default");
							textField_4.setColumns(10);
						}
						{
							JLabel lblNewLabel_8 = new JLabel("Reqired索引：");
							panel_1.add(lblNewLabel_8, "7, 6, right, default");
						}
						{
							textField_5 = new JTextField();
							panel_1.add(textField_5, "8, 6, fill, default");
							textField_5.setColumns(10);
						}
						{
							JPanel panel_2 = new JPanel();
							panel_1.add(panel_2, "2, 8, 7, 1, fill, fill");
							panel_2.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
							{
								JButton btnNewButton_1 = new JButton("导入");
								btnNewButton_1.addActionListener(new ActionListener() {
									@Override
									public void actionPerformed(ActionEvent arg0) {
										new Thread(new Runnable() {
											
											@Override
											public void run() {
												RemarkGetter remarkGetter = new RemarkGetter();
												remarkGetter.remarks(textField_2.getText(),
														textField_3.getText(),
														textField_1.getText(),
														ColumnIndex.builder()
															.codeIndex(Integer.parseInt(textField.getText()))
															.remarkIndex(Integer.parseInt(textField_4.getText()))
															.reqiredIndex(Integer.parseInt(textField_5.getText())).build());
												textArea2.setText(remarkGetter.toString());
											}
										}).start();
									}
								});
								panel_2.add(btnNewButton_1);
							}
						}
					}
				}
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("生成代码");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						JsonParser jsonParser = new JsonParser();
						ModelInfo modelInfo = new ModelInfo();
						modelInfo.setBeanName(txtTest.getText());
						modelInfo.setBeanPackage(packageText.getText());
						modelInfo.setMavenModel((String)comboBox.getSelectedItem());
						ResultBase result = jsonParser.doParse(textArea1.getText(), textArea2.getText(), modelInfo);
						if(result.isSuccess()) {
							JOptionPane.showMessageDialog(contentPanel, result.getMessage(), "结果", JOptionPane.INFORMATION_MESSAGE);
						} else {
							JOptionPane.showMessageDialog(contentPanel, result.getMessage(), "结果", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("确定");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
